<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace Catalog\Service;

use Catalog\Data\ProductDao;
use Catalog\Data\CategoryDao;
use Catalog\Data\LocalizedProductTextDao;
use Catalog\Data\ProductImageDao;
use Catalog\Data\ProductPriceDao;
use Catalog\Data\AttributeDao;

/**
 */
class ProductService
{

    /**
     */
    protected $productDao;

    /**
     */
    protected $localizedProductTextDao;
    
    /**
     */
    protected $categoryDao;

    /**
     */
    protected $productImageDao;
    
    /**
     */
    protected $productPriceDao;

    /**
     */
    protected $attributeDao;
    
    /**
     * 
     * @param ProductDao $productDao
     * @param CategoryDao $categoryDao
     * @param LocalizedProductTextDao $localizedProductTextDao
     * @param ProductImageDao $productImageDao
     * @param ProductPriceDao $productPriceDao
     * @param AttributeDao $attributeDao
     */
    public function __construct(ProductDao $productDao, CategoryDao $categoryDao, 
        LocalizedProductTextDao $localizedProductTextDao, ProductImageDao $productImageDao, ProductPriceDao $productPriceDao,
        AttributeDao $attributeDao)
    {
        $this->productDao = $productDao;
        $this->categoryDao = $categoryDao;
        $this->localizedProductTextDao = $localizedProductTextDao;
        $this->productImageDao = $productImageDao;
        $this->productPriceDao = $productPriceDao;
        $this->attributeDao = $attributeDao;
    }

    /**
     *
     * @param int $productId            
     */
    public function getProduct($productId)
    {
        return $this->productDao->get($productId);
    }
    
    /**
     * 
     * @param string $alias
     * @param string $language
     */
    public function getProductByAliasAndLanguage($alias, $language)
    {
    	$result = $this->localizedProductTextDao->getProductByAliasAndLanguage($alias, $language);
    	
    	return $result ? $result[0] : null;
    }
    
    /**
     * 
     * @param int $productId
     * @param string $language
     */
    public function getProductTexts($productId, $language) 
    {
    	return $this->localizedProductTextDao->get($productId, $language);
    }
    
    /**
     * 
     * @param int $productId
     * @param string $language
     */
    public function getProductImages($productId, $language)
    {
    	return $this->productImageDao->get($productId, $language);
    }
    
    /**
     * 
     * @param string $language
     */
    public function getCategories($language) 
    {
        return $this->categoryDao->getByLanguage($language);	
    }
    
    /**
     * 
     * @param string $categoryAlias
     * @param string $language
     * @param int $storeId
     * @param int $offset
     * @param int $maxResults
     */
    public function getProducts($categoryAlias, $language, $storeId, $offset, $maxResults)
    {
    	return $this->categoryDao->getProductsByCategoryAndLanguage($categoryAlias, $language, $storeId, $offset, $maxResults);
    }

    /**
     * 
     * @param string $categoryAlias
     * @param string $language
     */
    public function getCategoryName($categoryAlias, $language) 
    {
        $result = $this->categoryDao->getNameByAliasAndLanguage($categoryAlias, $language);
        
    	return $result ? $result[0] : null;
    }
    
    /**
     * 
     * @param int $storeId
     * @param int $productId
     */
    public function getProductPrice($storeId, $productId)
    {
        $result = $this->productPriceDao->get($storeId, $productId);
        
        return $result ? $result[0] : null;
    }
    
    /**
     *
     * @param string $categoryAlias
     * @param string $language
     */
    public function countProducts($categoryAlias, $language)
    {
    	return $this->categoryDao->countProductsByCategory($categoryAlias, $language);
    }
    
    /**
     * 
     * @param string $language
     * @param integer $pageSize
     * @param integer $storeId
     */
    public function getNewProducts($language, $pageSize, $storeId) 
    {
    	return $this->productDao->getNew($language, $pageSize, $storeId);
    }
    
    /**
     *
     * @param int $storeId
     * @param int $productId
     * @param string $type
     * @param string $language
     */
    public function getAttributeValues($storeId, $productId, $type, $language)
    {
    	return $this->attributeDao->getLocalizedValues($storeId, $productId, $type, $language);
    }
    
    /**
     *
     * @param int $productId
     * @param string $type
     * @param string $language
     */
    public function getAttribute($productId, $type, $language)
    {
        $result = $this->attributeDao->getLocalizedAttribute($productId, $type, $language);
        
        return $result ? $result[0] : null;
    }
    
}

?>